import { ElNotification } from "element-plus";

// 定义导出函数
// 导入 import { errorNotification } from "./utils";
// 调用 errorNotification()
export function errorNotification(msg: string) {
  ElNotification({
    title: "错误",
    message: msg,
    type: "error",
  });
}

// 定义导出函数，箭头函数
export const warningNotification = (msg: string) => {
  ElNotification({
    title: "警告",
    message: msg,
    type: "warning",
  });
};

export const nodeDebug = (title: string, ...params: any[]) => {
  let debug = require("debug");
  var appDebug = debug("app");
  appDebug(title, params);
};

export const convertToBoolean = (input: string): boolean | undefined => {
  try {
    return JSON.parse(input);
  } catch (e) {
    return undefined;
  }
};
// console.log(convertToBoolean("true")); // true
// console.log(convertToBoolean("false")); // false
// console.log(convertToBoolean("invalid")); // undefined

/**
 * {key1:value1, key2:value2} 转换为 [{name:key1, value:value1},{name:key2, value:value2}]
 * @param json {key1:value1, key2:value2}
 * @returns
 */
export const serializeArray = (json: object): Array<object> => {
  let list = Array<object>();
  for (let key in json) {
    // console.log("serializeArray", key);
    list.push({ name: key, value: json[key] });
  }
  return list;
};

// 导入 import utils from "./utils";
// 调用 utils.errorNotification()
export default {
  errorNotification,
  warningNotification,
  nodeDebug,
  convertToBoolean,
  serializeArray,
};
